package com.algrithom.pointer;

import java.util.Arrays;

/**
 * 443. 压缩字符串
 * 
 * @author think
 */
public class Solution5 {
    
    public static void main(String[] args){
        //        char[] chars = new char[]{'a','a','b','b','c','c','c'};
        char[] chars = new char[]{'a','b','b','b','b','b','b','b','b','b','b','b','b'};
        //        char[] chars = new char[]{'a'};
        Solution5 solution5 = new Solution5();
        System.out.println(solution5.compress(chars));
        System.out.println(Arrays.toString(chars));
    }
    
    public int compress(char[] chars){
        int len = chars.length;
        int left = 0;
        int right = left + 1;
        if (len == 1) {
            return 1;
        }
        StringBuilder stringBuilder = new StringBuilder();
        while (right < len) {
            while (right < len && chars[left] == chars[right]) {
                right++;
            }
            int val = right - left;
            stringBuilder.append(chars[left]).append(val == 1 ? "" : val);
            left = right;
        }
        System.arraycopy(stringBuilder.toString().toCharArray(),0,chars,0,stringBuilder.toString().length());
        return stringBuilder.toString().length();
    }
}
